//
//  MYGMedicalToolsAreaController.swift
//  MYGrow
//
//  Created by 林矗 on 2024/4/18.
//
import UIKit

class MYGMedicalToolsAreaController: MYGBaseController {
    // MARK: - Property
    private var height: Float?
    private var weight: Float?
    private var sex: Int?
    // MARK: - life cycle
    override func viewDidLoad() {
        super.viewDidLoad()
        configUI()
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        view.endEditing(true)
    }
    
    // MARK: - UI
    private func configUI() {
        view.backgroundColor = .white
        let separator1 = createSeparator()
        let separator2 = createSeparator()
        let separator3 = createSeparator()
        let separator4 = createSeparator()
        let separator5 = createSeparator()
        let separator6 = createSeparator()
        view.addSubviews([
            areaTitleLabel,
            areaTF,
            separator1,
            infoLabel,
            separator2,
            heightInputView,
            separator3,
            weightInputView,
            separator4,
            sexChooseView,
            separator5,
            separator6,
            commitButton
        ])
        
        areaTitleLabel.snp.makeConstraints { make in
            make.top.equalTo(26.5)
            make.left.equalTo(26)
            make.height.equalTo(22.5)
        }
        
        areaTF.snp.makeConstraints { make in
            make.top.equalTo(areaTitleLabel.snp.bottom).offset(15)
            make.left.equalTo(26)
            make.height.equalTo(22)
        }
        
        separator1.snp.makeConstraints { make in
            make.top.equalTo(areaTF.snp.bottom).offset(7.5)
            make.left.right.equalToSuperview().inset(26)
            make.height.equalTo(0.5)
        }
        
        infoLabel.snp.makeConstraints { make in
            make.top.equalTo(separator1.snp.bottom).offset(15)
            make.left.right.equalToSuperview().inset(26)
        }
    

        separator2.snp.makeConstraints { make in
            make.top.equalTo(infoLabel.snp.bottom).offset(15)
            make.left.right.equalToSuperview()
            make.height.equalTo(0.5)
        }
        
        heightInputView.snp.makeConstraints { make in
            make.top.equalTo(separator2.snp.bottom).offset(0)
            make.height.equalTo(56)
            make.left.right.equalToSuperview().inset(26)
        }
        
        separator3.snp.makeConstraints { make in
            make.top.equalTo(heightInputView.snp.bottom).offset(10)
            make.left.right.equalToSuperview().inset(26)
            make.height.equalTo(0.5)
        }
        
        weightInputView.snp.makeConstraints { make in
            make.top.equalTo(separator3.snp.bottom).offset(0)
            make.height.equalTo(56)
            make.left.right.equalToSuperview().inset(26)
        }
        
        separator4.snp.makeConstraints { make in
            make.top.equalTo(weightInputView.snp.bottom).offset(10)
            make.left.right.equalToSuperview().inset(26)
            make.height.equalTo(0.5)
        }
        
        sexChooseView.snp.makeConstraints { make in
            make.top.equalTo(separator4.snp.bottom).offset(0)
            make.height.equalTo(56)
            make.left.right.equalToSuperview().inset(26)
        }
        
        separator5.snp.makeConstraints { make in
            make.top.equalTo(sexChooseView.snp.bottom).offset(10)
            make.left.right.equalToSuperview().inset(26)
            make.height.equalTo(0.5)
        }
    
        commitButton.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.height.equalTo(49)
            make.bottom.equalToSuperview().offset(-bottomSafeheight)
        }
    }
    
    func createSeparator() -> UIView {
        let view = UIView()
        view.backgroundColor = .rgba(238, 238, 238)
        return view
    }
    
    // MARK: - lazy load
    private lazy var areaTitleLabel: UILabel = {
        let label = UILabel()
        label.font = .medium(16)
        label.textColor = .rgba(51, 51, 51)
        label.text = "体表面积"
        return label
    }()
    
    private lazy var areaTF: UITextField = {
        let tf = UITextField()
        tf.font = .regular(14)
        tf.textColor = .rgba(51, 51, 51)
        tf.placeholder = "输入数据后，进行计算"
        tf.setPlaceHolderTextColor(.rgba(204, 204, 204))
        tf.isEnabled = false
        return tf
    }()
    
    private lazy var heightInputView: MYGMedicalToolsInputView = {
        let view = MYGMedicalToolsInputView(type: .height, frame: .zero)
        view.inputDidChange = {[weak self] (height) in
            self?.height = Float(height)
        }
        return view
    }()
    
    private lazy var weightInputView: MYGMedicalToolsInputView = {
        let view = MYGMedicalToolsInputView(type: .weight, frame: .zero)
        view.inputDidChange = {[weak self] (weight) in
            self?.weight = Float(weight)
        }
        return view
    }()
    
    private lazy var sexChooseView: MYGMedicalToolsChooseView = {
        let view = MYGMedicalToolsChooseView(frame: .zero)
        view.title = "性别"
        view.leftTitle = "男"
        view.rightTitle = "女"
        view.selectedAction = {[weak self] (sex) in
            self?.sex = sex
        }
        return view
    }()
 
    private lazy var infoLabel: UILabel = {
        let label = UILabel()
        label.font = .medium(12)
        label.textColor = .rgba(153, 153, 153)
        label.text = "婴儿和儿童的表面积相对较小，成年人的表面积通常在 1.5-2.0 平方米之间，老年人的表面积可能会略微减小。"
        label.numberOfLines = 0
        return label
    }()
    
    private lazy var commitButton: UIButton = {
        let button = UIButton(type: .custom)
        button.backgroundColor = .rgba(151, 179, 255)
        button.setTitle("开始计算", for: .normal)
        button.setTitleColor(.white, for: .normal)
        button.titleLabel?.font = .medium(17)
        button.addTarget(self, action: #selector(commit), for: .touchUpInside)
        return button
    }()
    
}

extension MYGMedicalToolsAreaController {
    @objc func commit() {
        view.endEditing(true)
        guard let height = height, let weight = weight, let sex = sex  else { return }
        if height <= 0 { return }
        if weight < 0 || weight > 500 { return }
        
        
        let value = 0.0061 * height + 0.0128 * weight - 0.1529
        let text = "\(value) ㎡" as NSString
        var att = NSMutableAttributedString(string: text as String, attributes: [.font: UIFont.bold(20), .foregroundColor: UIColor.rgba(51, 51, 51)])
        att.setAttributes([.font: UIFont.medium(15), .foregroundColor: UIColor.rgba(102, 102, 102)], range: text.range(of: "㎡"))
        areaTF.attributedText = att
    }
}
